/*
 *  CANHAS.cpp
 *  TubeTruck
 *
 *  Created by Aeturnum on 6/4/08.
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
 *
 */

#include "CANHAS.h"

using namespace TTP;
using namespace std;

CANHAS::CANHAS() : Packet(Packet_CANHAS)
{}

CANHAS::CANHAS(id_hash idHash, std::vector<byte>& myKey, std::vector<byte>& groupKey) :
Packet(idHash, Packet_CANHAS),
mMyKey(myKey), mGroupKey(groupKey)
{}
CANHAS::~CANHAS() {}

vector<byte> CANHAS::getMyPublicKey() const{
   return mMyKey;
}

vector<byte> CANHAS::getGroupPublicKey() const{
   return mGroupKey;
}

// modifiers
void CANHAS::setMyPublicKey(vector<byte>& myKey) {
   mMyKey = myKey;
}

void CANHAS::setGroupPublicKey(vector<byte>& groupKey) {
   mGroupKey = groupKey;
}

// idHash
void CANHAS::setSourceIdHash(id_hash idHash) {
   mIdHash = idHash;
}

id_hash CANHAS::getSourceIdHash() const {
   return mIdHash;
}

//inherited
void CANHAS::fromVec(vector<byte>::const_iterator &vit, vector<byte>::const_iterator &end) {
   Packet::fromVec(vit, end);
   hashFromVec(vit);
   mMyKey.clear();
   mGroupKey.clear();
   
   for(unsigned short i = 0; i < Crypt::RSA::RSAKeyLength; i++)
      mMyKey.push_back(*vit++);
   
   for(unsigned short i = 0; i < Crypt::RSA::RSAKeyLength; i++)
      mGroupKey.push_back(*vit++);
   
   if(!verifyHash()) {
      throw msg_exception("CANHAS failed to varify hash value!!");
   }
}

void CANHAS::toVec(vector<byte> &vec) const {
   createHash();
   Packet::toVec(vec);
   hashToVec(vec);
   
   vec.insert(vec.end(), mMyKey.begin(), mMyKey.end());
   vec.insert(vec.end(), mGroupKey.begin(), mGroupKey.end());
}

vector<byte> CANHAS::getHashablePacket() const {
   vector<byte> vec;
   vector<byte> fakeHash(getBlankHash());
   
   Packet::toVec(vec);
   vec.insert(vec.end(), fakeHash.begin(), fakeHash.end());
   
   vec.insert(vec.end(), mMyKey.begin(), mMyKey.end());
   vec.insert(vec.end(), mGroupKey.begin(), mGroupKey.end());
   return vec;
}
